package com.huangliusong.web.template.engine;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.*;


/**
 * @author huangliusong
 * @since 2018/10/3
 * {@link }
 */
public class ThymeleafTemplateEngineBootstrap {
    public static void main(String[] args) throws IOException {
        //construct engine
        SpringTemplateEngine templateEngine=new SpringTemplateEngine();
        //create context
        Context context=new Context();
        context.setVariable("message","huangliusong");
        //read template content from hls.html
        //ResourceLoader
        ResourceLoader resourceLoader=new DefaultResourceLoader();
        Resource resource=resourceLoader.getResource("classpath:/template/thymeleaf/hls.html");
        File templateFile=  resource.getFile();
        //file stream
        FileInputStream inputStream=new FileInputStream(templateFile);
        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
        //copy
        IOUtils.copy(inputStream,outputStream);


        inputStream.close();

        //template content
        String content=outputStream.toString("UTF-8");
        //render result
        String result=templateEngine.process(content,context);
        //out the render result
        System.err.println(result);
    }


}
